home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / MYIO.CPP < prev    next >
C/C++ Source or Header  |  1993-06-14  |  4KB  |  141 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 06-13-93 (12:19)             Number: 137
  4. From: DAVID NUGENT                 Refer#: NONE
  5.   To: ALL                           Recvd: NO  
  6. Subj: [06 of 12] Myio.cpp            Conf: (37) C++ Langua
  7. ---------------------------------------------------------------------------
  8. // Myio.cpp
  9. // Simple I/O class to demonstrate use of C++ iostream
  10. // facilities in a customised environment
  11. // Written by David L Nugent, June 1993
  12.  
  13. # include <iostream.h>
  14. # include <string.h>
  15. # include "Myio.h"
  16. # include "Mystream.h"
  17.  
  18. Myio::Myio (int sz)
  19.     : bufsize(sz), bufchars(0), bufidx(0),
  20.       bufaddr(new char[bufsize]), mystream(0)
  21. {}
  22.  
  23. Myio::~Myio (void)
  24. {
  25.     delete bufaddr;
  26.     delete mystream;
  27. }
  28.  
  29. iostream &
  30. Myio::stream (void)
  31. {
  32.     if (!mystream)      // Create a stream if required
  33.         mystream = new Mystream(this);
  34.     return *mystream;
  35. }
  36.  
  37. int         // Simple write function into a circular buffer
  38. Myio::write (char const * buf, int len)
  39. {
  40.     int avail = (bufsize - bufchars);   // See how many fit
  41.     if (len > avail)
  42.     {
  43.         len = avail;
  44.         stat |= Myio::overflow;         // Only partial write
  45.     }
  46.     else
  47.         stat &= ~Myio::overflow;
  48.     avail = bufsize - bufidx;           // Caculate room at end
  49.     if (avail > len)
  50.         avail = len;
  51.     if (avail)
  52.     {
  53.         memcpy (bufaddr + bufidx, buf, avail);
  54.         bufidx += avail;                // Update the put index
  55.         buf += avail;                   // And the input pointer
  56.     }
  57.     if (bufidx >= bufsize)              // Wrap buffer to start
  58.         bufidx = 0;
  59.     avail = len - avail;                // See if there is any more to go
  60.     if (avail)
  61.     {
  62.         memcpy (bufaddr + bufidx, buf, avail);
  63.         bufidx += avail;                // Update the put index
  64.     }
  65.     bufchars += len;
  66.     return (_pcount = len);
  67. }
  68.  
  69. int         // Simple read function from a circular buffer
  70. Myio::read (char * buf, int len)
  71. {
  72.     if (len > bufchars)                 // Adjust for available bytes
  73.     {
  74.         len = bufchars;
  75.         stat |= Myio::underflow;        // Got an underflow (partial read)
  76.     }
  77.     else
  78.         stat &= ~Myio::underflow;       // Clear underflow flag
  79.     int startidx = bufidx - bufchars;   // Determine start get position
  80.     if (startidx < 0)
  81.         startidx += bufsize;            // Adjust for wrap
  82.     int avail = bufsize - startidx;     // Check room at end of buffer
  83.     if (avail > len)                    // Adjust down if necessary
  84.         avail = len;
  85.     if (avail)                          // Copy first section
  86.     {
  87.         memcpy (buf, bufaddr + startidx, avail);
  88.         startidx += avail;              // Adjust start index
  89.         buf += avail;                   // Adjust output pointer
  90.     }
  91.     if (startidx >= bufsize)            // Wrap buffer to start
  92.         startidx = 0;
  93.     avail = len - avail;                // See if there is any more to go
  94.     if (avail)                          // If so, copy the rest
  95.         memcpy (buf, bufaddr + startidx, avail);
  96.     bufchars -= len;                    // Adjust character count
  97.     return (_gcount = len);
  98. }
  99.  
  100. Myio &
  101. operator<< (Myio & m, char const * ptr)
  102. {
  103.     m.write (ptr, strlen (ptr));
  104.     return m;
  105. }
  106.  
  107. int
  108. Myio::dump (void) const
  109. {
  110.     if (bufchars)
  111.     {
  112.         char * tmp = new char[bufchars + 2];
  113.         int idx = bufidx - bufchars;
  114.         if (idx < 0)
  115.             idx += bufsize;
  116.         for (int i = 0; i < bufchars; )
  117.         {
  118.             if (idx >= bufsize)
  119.                 idx = 0;
  120.             tmp[i++] = bufaddr[idx++];
  121.         }
  122.         if (i)
  123.         {
  124.             if (tmp[i-1] != '\n')   // Terminate with NL
  125.                 tmp[i++] = '\n';
  126.             tmp[i] = 0;
  127.             cout << "---\n"
  128.                  << tmp
  129.                  << "---\n";
  130.         }
  131.         delete tmp;
  132.     }
  133.     return bufchars;
  134. }
  135.  
  136. --- MaltEd 1.0.b5
  137.  * Origin: Unique Computing Pty Ltd (3:632/348)
  138. SEEN-BY: 1/211 11/2 4 13/13 101/1 109/25 114/5 123/19 124/1 153/752 154/40
  139. SEEN-BY: 154/77 157/110 159/100 125 140 180 270 430 575 950 203/23 209/209
  140. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2430/1 2440/5 3603/20
  141.